package com.twohand.utils.Config;

import com.twohand.utils.Response.Response;
import com.twohand.utils.Response.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

@Slf4j
public class BaseBeforeResponse implements ResponseBodyAdvice {
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }


    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        //        log.info(Objects.requireNonNull(request.getMethod()).name());
        if(body instanceof String){
            //字符串
//            log.info("string");
            return body;
        }else if(body != null){
            //对象且没有code和message属性
            if(has(body,"code") && has(body,"message")){
//                log.info("已经存在结构体");
                log.info("response:{}",body);
                return body;
            }else if(body instanceof List){
                //数组没包装
//                log.info("数组没包装");
                Object res = Response.defaultOk(ResponseCode.RC200,body);
                log.info("response:{}","直接返回的数组不打印log");
                return res;
            }else if(body instanceof Boolean){
                log.info("response:{}",body);
                //判断是布尔值的情况
                String requestMethod = request.getMethod().name();
                switch (requestMethod) {
                    case "GET":
                    case "POST":
                        return Response.success(ResponseCode.RC201, (Boolean) body ? ResponseCode.sAdd : ResponseCode.fAdd, body);
                    case "PUT":
//                        log.info("put");
//                        log.info(ResponseCode.sMod);
                        return Response.success(ResponseCode.RC201, (Boolean) body ? ResponseCode.sMod : ResponseCode.fMod, body);
                    case "DELETE":
                        return Response.success(ResponseCode.RC204, (Boolean) body ? ResponseCode.sDel : ResponseCode.fDel, body);
                }
            }else{
//                log.info("不存在结构体");
                Object res = Response.success(body);
                log.info("response:{}",res);
                return res;
            }
        }
//        log.info("这是null");
        log.info("response:null");
        return null;
    }

    public Boolean has(Object object,String prototype){
        Field fields[] = object.getClass().getDeclaredFields();
//        log.info(fields.length);
        for(Field field : fields){
//            log.info(field.getName());
            if(field.getName().equals(prototype)){
                return true;
            }
        }
        return false;
    }
}
